package org.michael.u.brg.i18n.litz;

import java.awt.*;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import javax.swing.*;

public class LiterizerMainUi extends JFrame {

    private JComboBox cboEncoding;
    private JTextArea txtEncoding;
    private JTextArea txtVisual;

    private final HashMap<String, LitzXlator> xlators = new HashMap<String, LitzXlator>();
    private LitzXlator xlator;
    private final ArrayList<String> xlatorNames = new ArrayList<String>();

    public LiterizerMainUi() throws Exception {
        registerTranslator(new LitzUnicode());
        registerTranslator(new LitzRu());
        registerTranslator(new LitzHe());
        initComponents();
    }

    private void registerTranslator(LitzXlator x)
            throws Exception {
        if (xlators.get(x.getName()) != null) {
            throw new Exception("Translator \"" + x.getName() + "\" is already registered");
        } else {
            xlators.put(x.getName(), x);
            xlatorNames.add(x.getName());
        }
    }

    private void initComponents() {
        cboEncoding = new JComboBox();
        txtEncoding = new JTextArea();
        txtVisual = new JTextArea();
        final JMenuBar mainMenuBar = new JMenuBar();
        final JMenu mnuFile = new JMenu();
        final JSeparator mnuFileSepExit = new JSeparator();
        final JMenuItem mnuFileExit = new JMenuItem();
        final JMenu mnuXlate = new JMenu();
        final JMenuItem mnuXlateEncoding = new JMenuItem();
        final JMenuItem mnuXlateVisual = new JMenuItem();
        getContentPane().setLayout(new BorderLayout(2, 2));
        setDefaultCloseOperation(3);
        setTitle("MB\251 Literizer\u2122 v 0.1");
        setFont(new Font("Times New Roman", 0, 10));
        setName("fraMain");
        cboEncoding.setFont(new Font("Times New Roman", 0, 13));
        String xlatorNamesArray[] = new String[xlatorNames.size()];
        xlatorNames.toArray(xlatorNamesArray);
        cboEncoding.setModel(new DefaultComboBoxModel(xlatorNamesArray));
        xlator = xlators.get(xlatorNamesArray[0]);
        cboEncoding.setActionCommand("cboEncodingChanged");
        cboEncoding.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent evt) {
                cboEncodingActionPerformed(evt);
            }
        }
        );
        getContentPane().add(cboEncoding, "North");
        txtEncoding.setFont(new Font("Courier", 0, 18));
        txtEncoding.setRows(15);
        txtEncoding.setText("\\u05E9\\u05DC\\u05D5\\u05DD\\u0020\\u0429\\u0410\\u0020\\uFEB4\\uFEE0\\uFEE2");
        txtEncoding.setMinimumSize(new Dimension(22, 22));
        getContentPane().add(txtEncoding, "Center");
        txtVisual.setFont(new Font("Courier New", 0, 18));
        txtVisual.setRows(15);
        txtVisual.setText("Visual");
        txtVisual.setMinimumSize(getPreferredSize());
        txtVisual.setPreferredSize(getPreferredSize());
        getContentPane().add(txtVisual, "South");
        mnuFile.setMnemonic('F');
        mnuFile.setText("File");
        mnuFile.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent evt) {
                mnuFileActionPerformed(evt);
            }

        }
        );
        mnuFile.add(mnuFileSepExit);
        mnuFileExit.setAccelerator(KeyStroke.getKeyStroke(81, 2));
        mnuFileExit.setMnemonic('x');
        mnuFileExit.setText("Exit");
        mnuFileExit.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent evt) {
                mnuFileExitActionPerformed(evt);
            }
        }
        );
        mnuFile.add(mnuFileExit);
        mainMenuBar.add(mnuFile);
        mnuXlate.setMnemonic('T');
        mnuXlate.setText("Translate");
        mnuXlateEncoding.setAccelerator(KeyStroke.getKeyStroke(69, 2));
        mnuXlateEncoding.setMnemonic('E');
        mnuXlateEncoding.setText("Encoding");
        mnuXlateEncoding.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent evt) {
                mnuXlateEncodingActionPerformed(evt);
            }
        }
        );
        mnuXlate.add(mnuXlateEncoding);
        mnuXlateVisual.setAccelerator(KeyStroke.getKeyStroke(87, 2));
        mnuXlateVisual.setMnemonic('V');
        mnuXlateVisual.setText("Visual");
        mnuXlateVisual.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent evt) {
                mnuXlateVisualActionPerformed(evt);
            }
        }
        );
        mnuXlate.add(mnuXlateVisual);
        mainMenuBar.add(mnuXlate);
        setJMenuBar(mainMenuBar);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width - 753) / 2, (screenSize.height - 639) / 2, 753, 639);
    }

    @SuppressWarnings({"UnusedParameters"})
    private void mnuFileActionPerformed(ActionEvent evt) {
    }

    private void mnuXlateVisualActionPerformed(ActionEvent evt) {
        if (evt == null) return;
        final String encoding = xlator.getEncoding(txtVisual.getText());
        if (encoding == null) return;
        try {
            txtEncoding.setText(encoding);
            setClipboard(txtEncoding.getText());
        } catch (Exception x) {
            txtEncoding.setText(x.getMessage());
        }
    }

    private void mnuXlateEncodingActionPerformed(ActionEvent evt) {
        if (evt == null)
            return;
        String visual;
        visual = xlator.getVisual(txtEncoding.getText());
        if (visual == null)
            return;
        try {
            txtVisual.setText(visual);
            setClipboard(txtVisual.getText());
        } catch (Exception x) {
            txtVisual.setText(x.getMessage());
        }
    }

    private void cboEncodingActionPerformed(ActionEvent evt) {
        if (evt != null) {
            xlator = xlators.get(cboEncoding.getSelectedItem().toString());
            System.out.println("Translator changed to " + xlator.getName());
        }
    }

    private void mnuFileExitActionPerformed(ActionEvent evt) {
        if (evt != null) {
            System.exit(0);
        }
    }

    private void setClipboard(String what) {
        Toolkit tool = Toolkit.getDefaultToolkit();
        StringSelection ss = new StringSelection(what);
        tool.getSystemClipboard().setContents(ss, ss);
    }

    public static void main(String args[]) {
        try {
            LiterizerMainUi gui = new LiterizerMainUi();
            gui.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
